package GUI.Cliente;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import GUI.Cliente.Exceptions.ErrorEnBusqueda;
import client.entities.ClientVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.client.interfaces.ClientProxyMgt;

public class BuscarClienteCedula extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	int op;
	//op=0, busqueda para ver datos
	//op=1, busqueda para modificar la informacion
	//op=2 busqueda para eliminar el cliente

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public BuscarClienteCedula(Shell parent, int style, int o) {
		super(parent, style);
		
		if(o==0){
			setText("Buscar Cliente");
		}
		if (o==1){
			setText("Buscar Cliente a ser Modificado");
		}
		if(o==2){
			setText("Buscar Cliente a Eliminar");
		}
		
		op=o;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 300);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Label lblIngresarCedulaDel = new Label(shell, SWT.NONE);
		FormData fd_lblIngresarCedulaDel = new FormData();
		fd_lblIngresarCedulaDel.top = new FormAttachment(0, 72);
		fd_lblIngresarCedulaDel.left = new FormAttachment(0, 29);
		lblIngresarCedulaDel.setLayoutData(fd_lblIngresarCedulaDel);
		lblIngresarCedulaDel.setText("Ingresar Cedula del Cliente");
		
		text = new Text(shell, SWT.BORDER);
		FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(lblIngresarCedulaDel, 155, SWT.RIGHT);
		fd_text.top = new FormAttachment(0, 67);
		fd_text.left = new FormAttachment(lblIngresarCedulaDel, 6);
		text.setLayoutData(fd_text);
		
		Button btnBuscar = new Button(shell, SWT.NONE);

		 shell.setDefaultButton(btnBuscar);
		btnBuscar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			
				int ci = Integer.parseInt((text.getText()));
				ClientProxyMgt uso = ServiceFacade.getInstance().getClientProxyMgt();
				ClientVO[] clients=null;
				try {
					clients = uso.getClients();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				ClientVO cliente = null;
				for (int i=0;i<clients.length;i++){
					
					if(clients[i].getClientId()==ci){
						cliente=clients[i];
					}
				}
				if(cliente==null){
					String m = "NO SE ENCONTRO NINGUN CLIENTE CON ESA CEDULA, INTENTELO DE NUEVO";
					ErrorEnBusqueda error = new ErrorEnBusqueda (shell, shell.getStyle(), "m");
					error.open();
					
				}
				else{
					if (op==0){
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), cliente, 3);
					shell.setVisible(false);
					d.open();
					shell.getParent().dispose();
				}
					if(op==1){
						DatosCliente d = new DatosCliente (shell, shell.getStyle(), cliente, 1);
						shell.setVisible(false);
						d.open();
						shell.getParent().dispose();
					}
					if(op==2){
						DatosCliente d = new DatosCliente (shell, shell.getStyle(), cliente, 2);
						shell.setVisible(false);
						d.open();
						shell.getParent().dispose();
						
						
					}
					
				
			}
				
			}
			
		});
		FormData fd_btnBuscar = new FormData();
		fd_btnBuscar.top = new FormAttachment(text, 26);
		fd_btnBuscar.left = new FormAttachment(text, 0, SWT.LEFT);
		fd_btnBuscar.right = new FormAttachment(0, 269);
		btnBuscar.setLayoutData(fd_btnBuscar);
		btnBuscar.setText("Buscar");
		
	}
	

}
